# Current, as of Thursday, May 28, 2020.
# rm(list = ls())
# setwd("~/Dropbox/Base Dropbox/List Experiment Paper (PS)/Conditional Accept/Replication Files (Carmines-Schmidt PS)/Design Effects Tests")
library(foreign)
library(car)
library(readstata13)
library(zeligverse)
library(ggplot2)
# install.packages("stargazer", dependencies = TRUE)
library(stargazer)
library(apsrtable)
#-----
# Read in both the 2008 and 2012 CCAP modules as separate dataframes. 
ccap.2008 = read.dta13("CarminesSchmidtPS-replication-2008.dta", generate.factors = TRUE)
ccap.2012 = read.dta13("CarminesSchmidtPS-replication-2012.dta", generate.factors = TRUE)
#-----
# CCAP 2008. 

# Subset the data to remove the individuals NOT assigned to one of the treatment conditions.
ccap.2008 = subset(ccap.2008, treatment!="")
table(ccap.2008$treatment)
# Includes only respondents assigned to the "African American candidate," "Control," 
# "Gay or Homosexual candidate," "Muslim candidate," or "Female candidate" LEs. 

# Indicator variable: assignment to ANY of the sensitive-item conditions. 
ccap.2008$sensitive.item = ifelse(ccap.2008$treatment!="Control", "Sensitive-Item", "Control")
table(ccap.2008$sensitive.item)
table(ccap.2008$sensitive.item, ccap.2008$treatment)
# Dichotomous, numeric version of this variable (1 = sensitive-item conditions; 0 = treatment condition).
ccap.2008$sensitive.item.binary = ifelse(ccap.2008$sensitive.item=="Sensitive-Item", 1, 0) 
table(ccap.2008$sensitive.item.binary)
table(ccap.2008$sensitive.item.binary, ccap.2008$treatment)

# AFRICAN AMERICAN or BASELINE conditions, 2008. Drop African Americans from the subsample. 
afroam.experiment = subset(ccap.2008, treatment=="African Americans" | treatment=="Control")
afroam.experiment = subset(afroam.experiment, black!=1)
# REPUBLICANS assigned to AFRICAN AMERICAN or BASELINE conditions, 2008. 
afroam.experiment.gop = subset(afroam.experiment, republican==1)
# DEMOCRATS assigned to AFRICAN AMERICAN or BASELINE conditions, 2008.  
afroam.experiment.dem = subset(afroam.experiment, democrat==1)
# INDEPENDENTS assigned to AFRICAN AMERICAN or BASELINE conditions, 2008. 
afroam.experiment.ind = subset(afroam.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to AFRICAN AMERICAN or BASELINE conditions, 2008. 
afroam.experiment.cons = subset(afroam.experiment, conservative==1)
# LIBERALS assigned to AFRICAN AMERICAN or BASELINE conditions, 2008. 
afroam.experiment.lib = subset(afroam.experiment, liberal==1)
# MODERATES assigned to AFRICAN AMERICAN or BASELINE conditions, 2008. 
afroam.experiment.mod = subset(afroam.experiment, (conservative==0 & liberal==0))

# MUSLIM or BASELINE conditions, 2008. 
muslims.experiment = subset(ccap.2008, treatment=="Muslims" | treatment=="Control")
# REPUBLICANS assigned to MUSLIM or BASELINE conditions, 2008. 
muslims.experiment.gop = subset(muslims.experiment, republican==1)
# DEMOCRATS assigned to MUSLIM or BASELINE conditions, 2008.  
muslims.experiment.dem = subset(muslims.experiment, democrat==1)
# INDEPENDENTS assigned to MUSLIM or BASELINE conditions, 2008. 
muslims.experiment.ind = subset(muslims.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to MUSLIM or BASELINE conditions, 2008. 
muslims.experiment.cons = subset(muslims.experiment, conservative==1)
# LIBERALS assigned to MUSLIM or BASELINE conditions, 2008. 
muslims.experiment.lib = subset(muslims.experiment, liberal==1)
# MODERATES assigned to MUSLIM or BASELINE conditions, 2008. 
muslims.experiment.mod = subset(muslims.experiment, (conservative==0 & liberal==0))

# 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2008. 
gays.experiment = subset(ccap.2008, treatment=="Gays" | treatment=="Control")
# REPUBLICANS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2008. 
gays.experiment.gop = subset(gays.experiment, republican==1)
# DEMOCRATS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2008.  
gays.experiment.dem = subset(gays.experiment, democrat==1)
# INDEPENDENTS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2008. 
gays.experiment.ind = subset(gays.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2008. 
gays.experiment.cons = subset(gays.experiment, conservative==1)
# LIBERALS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2008. 
gays.experiment.lib = subset(gays.experiment, liberal==1)
# MODERATES assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2008. 
gays.experiment.mod = subset(gays.experiment, (conservative==0 & liberal==0))

# WOMAN or BASELINE conditions, 2008. 
women.experiment = subset(ccap.2008, treatment=="Women" | treatment=="Control")
# REPUBLICANS assigned to WOMAN or BASELINE conditions, 2008. 
women.experiment.gop = subset(women.experiment, republican==1)
# DEMOCRATS assigned to WOMAN or BASELINE conditions, 2008.  
women.experiment.dem = subset(women.experiment, democrat==1)
# INDEPENDENTS assigned to WOMAN or BASELINE conditions, 2008. 
women.experiment.ind = subset(women.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to WOMAN or BASELINE conditions, 2008. 
women.experiment.cons = subset(women.experiment, conservative==1)
# LIBERALS assigned to WOMAN or BASELINE conditions, 2008. 
women.experiment.lib = subset(women.experiment, liberal==1)
# MODERATES assigned to WOMAN or BASELINE conditions, 2008. 
women.experiment.mod = subset(women.experiment, (conservative==0 & liberal==0))

# SENSITIVE-ITEM CONDITIONS or BASELINE conditions, 2008. 
# REPUBLICANS, full sample. 
republicans = subset(ccap.2008, republican==1)
# DEMOCRATS, full sample.
democrats = subset(ccap.2008, democrat==1)
# INDEPENDENTS, full sample. 
independents = subset(ccap.2008, (republican==0 & democrat==0))
# CONSERVATIVES, full sample.
conservatives = subset(ccap.2008, conservative==1)
# LIBERALS, full sample.
liberals = subset(ccap.2008, liberal==1)
# MODERATES, full sample. 
moderates = subset(ccap.2008, (conservative==0 & liberal==0))

# Generate p-values for the design effects tests for *all* of the subsamples above. 
library(list)
# First, make a list of datasets that we can call in a for-loop. 
subsample.datasets = list(afroam.experiment, afroam.experiment.cons, afroam.experiment.dem, afroam.experiment.gop, 
                          afroam.experiment.ind, afroam.experiment.lib, afroam.experiment.mod, gays.experiment, 
                          gays.experiment.cons, gays.experiment.dem, gays.experiment.gop, gays.experiment.ind, gays.experiment.lib, 
                          gays.experiment.mod, women.experiment, women.experiment.cons, women.experiment.dem, women.experiment.gop, 
                          women.experiment.ind, women.experiment.lib, women.experiment.mod, muslims.experiment, 
                          muslims.experiment.cons, muslims.experiment.dem, muslims.experiment.gop, muslims.experiment.ind, 
                          muslims.experiment.lib, muslims.experiment.mod, conservatives, democrats, republicans, independents, 
                          liberals, moderates, ccap.2008)
# For-loop to recover the unique p-values. 
set.seed(47408)
our.test = list(list())
p.design.effects.2008 = c()
for (i in 1:35) {
  our.test[[i]] = ict.test(subsample.datasets[[i]]$item_count, subsample.datasets[[i]]$sensitive.item.binary, 
                           J = 4, alpha = 0.05, n.draws = 250000, gms = TRUE, pi.table = FALSE) 
  p.design.effects.2008[i] = as.numeric(our.test[[i]]$p)
}

# Double-checking table. Names of the datasets we used...
# our.dataset.names = c("afroam.experiment", "afroam.experiment.cons", "afroam.experiment.dem", "afroam.experiment.gop", 
#                      "afroam.experiment.ind", "afroam.experiment.lib", "afroam.experiment.mod", "gays.experiment", 
#                      "gays.experiment.cons", "gays.experiment.dem", "gays.experiment.gop", "gays.experiment.ind", "gays.experiment.lib", 
#                      "gays.experiment.mod", "women.experiment", "women.experiment.cons", "women.experiment.dem", "women.experiment.gop", 
#                      "women.experiment.ind", "women.experiment.lib", "women.experiment.mod", "muslims.experiment", 
#                      "muslims.experiment.cons", "muslims.experiment.dem", "muslims.experiment.gop", "muslims.experiment.ind", 
#                      "muslims.experiment.lib", "muslims.experiment.mod", "conservatives", "democrats", "republicans", "independents", 
#                      "liberals", "moderates", "ccap.2008")
# cbind(our.dataset.names, round(p.design.effects.2008, 2))
#-----

#-----
# CCAP 2012. 

# Indicator variable: assignment to ANY of the sensitive-item conditions. 
ccap.2012$sensitive.item = ifelse(ccap.2012$treatment!="Control", "Sensitive-Item", "Control")
table(ccap.2012$sensitive.item)
table(ccap.2012$sensitive.item, ccap.2012$treatment)
# Dichotomous, numeric version of this variable (1 = sensitive-item conditions; 0 = treatment condition).
ccap.2012$sensitive.item.binary = ifelse(ccap.2012$sensitive.item=="Sensitive-Item", 1, 0) 
table(ccap.2012$sensitive.item.binary)
table(ccap.2012$sensitive.item.binary, ccap.2012$treatment)

# AFRICAN AMERICAN or BASELINE conditions, 2012. Drop African Americans from the subsample. 
afroam.experiment = subset(ccap.2012, treatment=="African Americans" | treatment=="Control")
afroam.experiment = subset(afroam.experiment, black!=1)
# REPUBLICANS assigned to AFRICAN AMERICAN or BASELINE conditions, 2012. 
afroam.experiment.gop = subset(afroam.experiment, republican==1)
# DEMOCRATS assigned to AFRICAN AMERICAN or BASELINE conditions, 2012.  
afroam.experiment.dem = subset(afroam.experiment, democrat==1)
# INDEPENDENTS assigned to AFRICAN AMERICAN or BASELINE conditions, 2012. 
afroam.experiment.ind = subset(afroam.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to AFRICAN AMERICAN or BASELINE conditions, 2012. 
afroam.experiment.cons = subset(afroam.experiment, conservative==1)
# LIBERALS assigned to AFRICAN AMERICAN or BASELINE conditions, 2012. 
afroam.experiment.lib = subset(afroam.experiment, liberal==1)
# MODERATES assigned to AFRICAN AMERICAN or BASELINE conditions, 2012. 
afroam.experiment.mod = subset(afroam.experiment, (conservative==0 & liberal==0))

# MUSLIM or BASELINE conditions, 2012. 
muslims.experiment = subset(ccap.2012, treatment=="Muslims" | treatment=="Control")
# REPUBLICANS assigned to MUSLIM or BASELINE conditions, 2012. 
muslims.experiment.gop = subset(muslims.experiment, republican==1)
# DEMOCRATS assigned to MUSLIM or BASELINE conditions, 2012.  
muslims.experiment.dem = subset(muslims.experiment, democrat==1)
# INDEPENDENTS assigned to MUSLIM or BASELINE conditions, 2012. 
muslims.experiment.ind = subset(muslims.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to MUSLIM or BASELINE conditions, 2012. 
muslims.experiment.cons = subset(muslims.experiment, conservative==1)
# LIBERALS assigned to MUSLIM or BASELINE conditions, 2012. 
muslims.experiment.lib = subset(muslims.experiment, liberal==1)
# MODERATES assigned to MUSLIM or BASELINE conditions, 2012. 
muslims.experiment.mod = subset(muslims.experiment, (conservative==0 & liberal==0))

# 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2012. 
gays.experiment = subset(ccap.2012, treatment=="Gays" | treatment=="Control")
# REPUBLICANS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2012. 
gays.experiment.gop = subset(gays.experiment, republican==1)
# DEMOCRATS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2012.  
gays.experiment.dem = subset(gays.experiment, democrat==1)
# INDEPENDENTS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2012. 
gays.experiment.ind = subset(gays.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2012. 
gays.experiment.cons = subset(gays.experiment, conservative==1)
# LIBERALS assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2012. 
gays.experiment.lib = subset(gays.experiment, liberal==1)
# MODERATES assigned to 'GAY OR HOMOSEXUAL' or BASELINE conditions, 2012. 
gays.experiment.mod = subset(gays.experiment, (conservative==0 & liberal==0))

# MORMON or BASELINE conditions, 2012. 
mormons.experiment = subset(ccap.2012, treatment=="Mormons" | treatment=="Control")
# REPUBLICANS assigned to MORMON or BASELINE conditions, 2012. 
mormons.experiment.gop = subset(mormons.experiment, republican==1)
# DEMOCRATS assigned to MORMON or BASELINE conditions, 2012.  
mormons.experiment.dem = subset(mormons.experiment, democrat==1)
# INDEPENDENTS assigned to MORMON or BASELINE conditions, 2012. 
mormons.experiment.ind = subset(mormons.experiment, (republican==0 & democrat==0))
# CONSERVATIVES assigned to MORMON or BASELINE conditions, 2012. 
mormons.experiment.cons = subset(mormons.experiment, conservative==1)
# LIBERALS assigned to MORMON or BASELINE conditions, 2012. 
mormons.experiment.lib = subset(mormons.experiment, liberal==1)
# MODERATES assigned to MORMON or BASELINE conditions, 2012. 
mormons.experiment.mod = subset(mormons.experiment, (conservative==0 & liberal==0))

# SENSITIVE-ITEM CONDITIONS or BASELINE conditions, 2012. 
# REPUBLICANS, full sample. 
republicans = subset(ccap.2012, republican==1)
# DEMOCRATS, full sample.
democrats = subset(ccap.2012, democrat==1)
# INDEPENDENTS, full sample. 
independents = subset(ccap.2012, (republican==0 & democrat==0))
# CONSERVATIVES, full sample.
conservatives = subset(ccap.2012, conservative==1)
# LIBERALS, full sample.
liberals = subset(ccap.2012, liberal==1)
# MODERATES, full sample. 
moderates = subset(ccap.2012, (conservative==0 & liberal==0))

# Generate p-values for the design effects tests for *all* of the subsamples above. 
library(list)
# First, make a list of datasets that we can call in a for-loop. 
subsample.datasets = list(afroam.experiment, afroam.experiment.cons, afroam.experiment.dem, afroam.experiment.gop, 
                          afroam.experiment.ind, afroam.experiment.lib, afroam.experiment.mod, gays.experiment, 
                          gays.experiment.cons, gays.experiment.dem, gays.experiment.gop, gays.experiment.ind, gays.experiment.lib, 
                          gays.experiment.mod, mormons.experiment, mormons.experiment.cons, mormons.experiment.dem, mormons.experiment.gop, 
                          mormons.experiment.ind, mormons.experiment.lib, mormons.experiment.mod, muslims.experiment, 
                          muslims.experiment.cons, muslims.experiment.dem, muslims.experiment.gop, muslims.experiment.ind, 
                          muslims.experiment.lib, muslims.experiment.mod, conservatives, democrats, republicans, independents, 
                          liberals, moderates, ccap.2012)
# For-loop to recover the unique p-values. 
set.seed(47408)
our.test = list(list())
p.design.effects.2012 = c()
for (i in 1:35) {
  our.test[[i]] = ict.test(subsample.datasets[[i]]$item_count, subsample.datasets[[i]]$sensitive.item.binary, 
                           J = 4, alpha = 0.05, n.draws = 250000, gms = TRUE, pi.table = FALSE) 
  p.design.effects.2012[i] = as.numeric(our.test[[i]]$p)
}
p.design.effects.2012

# Double-checking tables. Names of the datasets we used...
# our.dataset.names = c("afroam.experiment", "afroam.experiment.cons", "afroam.experiment.dem", "afroam.experiment.gop", 
#                      "afroam.experiment.ind", "afroam.experiment.lib", "afroam.experiment.mod", "gays.experiment", 
#                      "gays.experiment.cons", "gays.experiment.dem", "gays.experiment.gop", "gays.experiment.ind", "gays.experiment.lib", 
#                      "gays.experiment.mod", "mormons.experiment", "mormons.experiment.cons", "mormons.experiment.dem", "mormons.experiment.gop", 
#                      "mormons.experiment.ind", "mormons.experiment.lib", "mormons.experiment.mod", "muslims.experiment", 
#                      "muslims.experiment.cons", "muslims.experiment.dem", "muslims.experiment.gop", "muslims.experiment.ind", 
#                      "muslims.experiment.lib", "muslims.experiment.mod", "conservatives", "democrats", "republicans", "independents", 
#                      "liberals", "moderates", "ccap.2012")
# cbind(our.dataset.names, round(p.design.effects.2012, 2))
#-----

#-----
# Plot histograms of p-values from the "design effects" tests (Blair and Imai 2012).

# Dataframe: p-values and survey year (2008 or 2012)
our.p.values = c(p.design.effects.2008, p.design.effects.2012)
year.of.survey = c(rep("2008 CCAP", 35), rep("2012 CCAP", 35))
histogram.data = data.frame(our.p.values, year.of.survey)

# Histogram plot. 
library(graphics)
our.plot = ggplot(histogram.data, aes(x = our.p.values)) + 
  geom_histogram(aes(y = ..density..), color = "black", fill = "grey", binwidth = 0.10) + 
  facet_wrap(~ year.of.survey) + 
  geom_vline(aes(xintercept = 0.05), linetype = "dashed") + 
  theme_bw() + theme_minimal() + xlab("Design Effects Test, p-value (see Blair and Imai 2012)") + 
  ylab("Density") # + ggtitle("Figure 1: Density Histograms of p-values from Design Effects Tests, \nAll Samples and Subsamples Analyzed (Including Partisan and Ideological Subgroups)")
our.plot = our.plot +
  annotate('text', x = 0.06, y = 5.3, hjust = "inward", label = "{alpha}==0.05", parse = TRUE, size = 4) +
  annotate('text', x = 0.06, y = 5, hjust = "inward", label = "Level for rejecting null hypothesis of no design effects", size = 3.7) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) + 
  theme(strip.text = element_text(size = 12, face = "bold"))
our.plot
ggsave(plot = our.plot, file = "Fig1.eps", height = 5, width = 8.5, units = "in")
# ggsave("Figure-1-design-effects-tests.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))

